<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章类型" prop="articleType">
        <el-select v-model="queryParams.articleType" placeholder="请选择文章类型" clearable>
          <el-option
            v-for="dict in dict.type.information_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布用户" prop="releaseName">
        <el-select v-model="queryParams.releaseName" allow-create filterable placeholder="请选择用户">
          <el-option
            v-for="dict in userList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditState">
        <el-select v-model="queryParams.auditState" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:article:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:article:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:article:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange" height="600" fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="文章类型" align="center" prop="articleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.information_type" :value="scope.row.articleType"/>
        </template>
      </el-table-column>
      <el-table-column label="文章标题" align="center" prop="title"/>
      <el-table-column label="发布用户" align="center" prop="releaseName"/>
      <el-table-column label="链接类型" align="center" prop="linkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.link_type" :value="scope.row.linkType"/>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="auditState" width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditState"/>
        </template>
      </el-table-column>
      <el-table-column width="220"  label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleLook(scope.row.id)"
            >预览
            </el-button>
            <el-button
              v-if="scope.row.auditState==0"
              style=""
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleAudit(scope.row)"
              v-hasPermi="['information:article:process']"
            >审核
            </el-button>
            <el-button
              v-if="scope.row.auditState==3"
              style=""
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleAudit(scope.row)"
              v-hasPermi="['information:article:process2']"
            >审核
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleRecom(scope.row)"
              v-hasPermi="['information:article:edit']"
              v-if="scope.row.state===0"
            >禁用
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleRecom(scope.row)"
              v-hasPermi="['information:article:edit']"
              v-if="scope.row.state===1"
            >启用
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['information:article:edit']"
            ><span v-if="$store.getters.userId===scope.row.sort">编辑</span>
              <span v-if="$store.getters.userId!==scope.row.sort">查看</span>
            </el-button>
            <el-button
              style=""
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['information:article:remove']"
            >删除
            </el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资讯对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="759px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="文章类型" prop="articleType">
          <el-select class="kind" v-model="form.articleType" placeholder="请选择文章类型">
            <el-option
              v-for="dict in dict.type.information_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章标题" prop="title">
          <el-input class="kind" v-model="form.title" placeholder="请输入文章标题"/>
        </el-form-item>
        <el-form-item label="发布用户" prop="governId">
          <el-select class="kind" v-model="form.governId" allow-create @change="changeGovernId" filterable
                     placeholder="请选择用户">
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <image-upload :limit="1" v-model="form.cover"/>
        </el-form-item>
        <el-form-item label="链接类型" prop="linkType">
          <el-select class="kind" v-model="form.linkType" @change="changeType" placeholder="请选择链接类型">
            <el-option
              v-for="dict in dict.type.link_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.linkType===1" label="内容">
          <MyEditor class="kind" v-model="form.linkVal" :min-height="192"/>
        </el-form-item>
        <el-form-item v-if="form.linkType===2" label="内容">
          <el-input class="kind" v-model="form.linkVal"/>
        </el-form-item>
        <el-form-item label="摘要" prop="note">
          <el-input class="kind" v-model="form.note" type="textarea" placeholder="请输入摘要"/>
        </el-form-item>
        <el-form-item v-if="form.auditState!==0" label="审核备注" prop="remark">
          <el-input class="kind" disabled type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="$store.getters.userId===form.sort" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="open2" close-on-click-modal center width="750px">
      <el-form :model="form" label-width="130px">
        <el-form-item label="审核备注" prop="remark">
          <el-input class="kind" type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="success" @click="submitCheck(1)">审核通过</el-button>-->
        <el-button type="success" v-if="form.auditState===0" @click="submitCheck(3)">审核通过</el-button>
        <el-button type="success" v-if="form.auditState===3" @click="submitCheck(1)">审核通过</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="danger" v-if="form.auditState===0" @click="submitCheck(2)">审核驳回</el-button>
        <el-button type="danger" v-if="form.auditState===3" @click="submitCheck(4)">审核驳回</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="info" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {addArticle, delArticle, getArticle, listArticle, updateArticle} from "@/api/information/article";
import {listUserAll} from "@/api/information/user";
import MyEditor from "@/components/wangEdiot/index.vue";

export default {
  name: "Article",
  components: {MyEditor},
  dicts: ['link_type', 'information_type','audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资讯表格数据
      articleList: [],
      userList: [],
      // 弹出层文章标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      status: [
        {
          value: 0,
          label: "初审"
        },
        {
          value: 1,
          label: "审批通过"
        },
        {
          value: 2,
          label: "审批失败"
        },
        {
          value: 3,
          label: "复审"
        },
      ],
      // 查询参数
      queryParams: {
        auditState: null,
        pageNum: 1,
        pageSize: 10,
        articleType: null,
        title: null,
        releaseName: null,
        releaseTime: null,
        linkType: null,
        linkVal: null,
        intro: null,
        governId: null,
        downloadUrl: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        num: null,
        cover: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleType: [
          {required: true, message: "文章类型必须选择", trigger: "blur"}
        ],
        title: [
          {required: true, message: "文章标题不能为空", trigger: "blur"},
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        cover: [
          {required: true, message: "封面不能为空", trigger: "blur"}
        ],
        intro: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        linkVal: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        governId: [
          {required: true, message: "发布用户不能为空", trigger: "blur"}
        ],
        linkType: [
          {required: true, message: "链接类型不能为空", trigger: "change"}
        ],
        auditState: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        remark: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        note: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    handleLook(id) {
      // window.location.href=`http://120.46.220.152/#/pages/news/details?id=`+id
      open(`http://120.46.220.152/#/pages/news/details?id=` + id)
    },
    /** 提交审核 */
    submitCheck(state) {
      updateArticle({
        id: this.form.id,
        auditState: state,
        remark: this.form.remark
      }).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open2 = false;
        this.getList();
      });
    },
    changeType() {
      this.form.linkVal = null
    },
    changeGovernId() {
      this.form.releaseName = null;
      this.userList.forEach(item => {
        if (item.userId === this.form.governId) {
          this.form.releaseName = item.nickName
        }
      })
      if (this.form.releaseName == null) {
        this.form.releaseName = this.form.governId
      }
      console.log(Number(this.form.governId))
    },
    /** 启禁用按钮操作 */
    handleRecom(row) {
      const id = row.id
      if (row.state === 0) {
        this.$modal.confirm('是否确认禁用编号为"' + id + '"的数据项？').then(function () {
          row.state = 1
          return updateArticle(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("禁用成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否确认启用编号为"' + id + '"的数据项？').then(function () {
          row.state = 0
          return updateArticle(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        }).catch(() => {
        });
      }

    },
    getUserList() {
      listUserAll().then(response => {
        this.userList = response.data
      })
    },
    /** 查询资讯列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        articleType: null,
        title: null,
        releaseName: null,
        releaseTime: null,
        linkType: null,
        linkVal: null,
        intro: null,
        governId: null,
        downloadUrl: null,
        note: null,
        sort: this.$store.getters.userId,
        state: null,
        dr: 0,
        createTime: null,
        updateTime: null,
        num: null,
        cover: null,
        remark: null,
        auditState: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.linkType = 1
      this.title = "添加资讯";
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      const id = row.id
      getArticle(id).then(response => {
        this.form = response.data;
        this.open2 = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data;
        let a = 0;

        this.userList.forEach(item => {
          if (item.userId === this.form.governId) {
            if (this.form.releaseName === item.nickName) {
              a = 1
            }
          }
        })
        if (a === 0) {
          this.form.governId = this.form.releaseName;
        }
        this.open = true;
        if (this.$store.getters.userId !== this.form.sort) {
          this.title = "资讯详情";
        }else {
          this.title = "修改资讯";
        }

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          if (isNaN(Number(this.form.governId))) {
            this.form.governId = 0;
          }

          if (this.form.id != null) {
            this.form.auditState = 0
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资讯编号为"' + ids + '"的数据项？').then(function () {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
